package com.jiyejia.utils;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.jiyejia.config.Const;
import com.jiyejia.config.NetworkUrl;
import com.jiyejia.model.Order;

public class NetWorkManager {
	public static final String TAG = "NetWorkManager";
	private static NetWorkManager mNet;

	public static NetWorkManager getInstance() {
		if (mNet == null) {
			mNet = new NetWorkManager();
		}
		return mNet;
	}

	/**
	 * 添加订单
	 * 
	 * @param mArray
	 */
	public void postAddOrder(ArrayList<Order> mArray) {
		try {
			HttpPost request = new HttpPost(NetworkUrl.postOrderListUrl);
			request.addHeader("Content-Type", "application/json");
			request.addHeader("charset", HTTP.UTF_8);

			// 先封装一个 JSON 对象
			JSONObject json = new JSONObject();
			for (Order order : mArray) {
				JSONObject param = new JSONObject();
				param.put("id", order.mOrderId);
				param.put("name", order.mOrderName);
				param.put("count", order.mOrderCount);
				JSONArray array = new JSONArray();
				array.put(param);
				json.put("orderDTOs", array);
			}
			// 绑定到请求 Entry
			StringEntity se = new StringEntity(json.toString(), HTTP.UTF_8);
			se.setContentType("application/json");
			request.setEntity(se);
			Log.i(TAG, json.toString());
			// 发送请求
			HttpResponse httpResponse = null;
			httpResponse = new DefaultHttpClient().execute(request);
			// 得到应答的字符串，这也是一个 JSON 格式保存的数据
			String retSrc = EntityUtils.toString(httpResponse.getEntity());
			// 生成 JSON 对象
			JSONObject result = new JSONObject(retSrc);
			Log.i(TAG, result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取所有商品
	 */
	public ArrayList<Order> postGetAllGoods() {
		ArrayList<Order> list = new ArrayList<Order>();
		try {
			HttpPost request = new HttpPost(NetworkUrl.postGetAllGoods);
			request.addHeader("charset", HTTP.UTF_8);
			// 发送请求
			HttpResponse httpResponse = null;
			httpResponse = new DefaultHttpClient().execute(request);
			// 得到应答的字符串，这也是一个 JSON 格式保存的数据
			String retSrc = EntityUtils.toString(httpResponse.getEntity());
			// 生成 JSON 对象
			JSONObject result = new JSONObject(retSrc);
			Log.i(TAG, result.toString());
			JSONArray goodsArray = new JSONArray(result.getString(Const.jsonResult));
			for (int i = 0; i < goodsArray.length(); i++) {
				JSONObject jo = goodsArray.getJSONObject(i);
				Order o = new Order();
				o.mOrderCount = jo.getInt(Const.count);
				o.mOrderId = jo.getInt(Const.id);
				o.mOrderName = jo.getString(Const.name);
				Log.i(TAG, "GetAllGoods json " + i + " is " + jo.toString()+"``````"+o.toString());
				list.add(o);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 搜索商品
	 * 
	 * @param name
	 * @return
	 */
	public ArrayList<Order> postSearchGoods(String name) {
		ArrayList<Order> list = new ArrayList<Order>();
		try {
			HttpPost request = new HttpPost(NetworkUrl.postSearchGoods + name);
			request.addHeader("charset", HTTP.UTF_8);
			// 发送请求
			HttpResponse httpResponse = null;
			httpResponse = new DefaultHttpClient().execute(request);
			// 得到应答的字符串，这也是一个 JSON 格式保存的数据
			String retSrc = EntityUtils.toString(httpResponse.getEntity());
			// 生成 JSON 对象
			JSONObject result = new JSONObject(retSrc);
			Log.i(TAG, result.toString());
			JSONArray goodsArray = result.getJSONArray(Const.jsonResult);
			for (int i = 0; i < goodsArray.length(); i++) {
				JSONObject jo = goodsArray.getJSONObject(i);
				Log.i(TAG, "SearchGoods json " + i + " is" + jo.toString());
				Order o = new Order();
				o.mOrderCount = jo.getInt(Const.count);
				o.mOrderId = jo.getInt(Const.id);
				o.mOrderName = jo.getString(Const.name);
				list.add(o);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}

}
